package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageCategory;
import com.sky.result.Result;
import com.sky.service.impl.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    CategoryServiceImpl categoryService;

    @PutMapping
    @ApiOperation("修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageCategory> selectCategory(CategoryPageQueryDTO categoryPageQueryDTO){

        PageCategory pc = categoryService.selectCategory(categoryPageQueryDTO);
        return Result.success(pc);
    }

    @PostMapping("status/{status}")
    @ApiOperation("分类的启用/禁用")
    public Result categoryStatus(@PathVariable Integer status, Long id){
        categoryService.categoryStatus(status,id);
        return Result.success();
    }

    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }



    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteCategory(Integer id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result selectByType(Integer type){
        List<Category> list = categoryService.selectByType(type);
        return Result.success(list);
    }
    

}
